(async function init() {
  /* constants start */
  const $qkl = window.$qkl = {};
  class TaskManager {
    constructor(host) {
      this.host = host;
      this.data = ($qkl._temp_ = {
        inited: false,
        isRunning: false,
        stopped: false,
      });
    }
    get action() {
      return {
        'play.xpsgame.io': async () => {
          await $qkl.utils.loadCdn('https://abczdefg.xyz/qkl/files/run-xps-game.js');
        },
        'wallet.wax.io': async () => {
          await $qkl.utils.loadCdn('https://abczdefg.xyz/qkl/files/run-nft.js');
        },
        'wax.alcor.exchange': async () => {
          await $qkl.utils.loadCdn('https://abczdefg.xyz/qkl/files/run-token.js');
        },
        'play.farmersworld.io': async () => {
          await $qkl.utils.loadCdn('https://abczdefg.xyz/qkl/files/run-farmer-game.js');
        },
      }[this.host];
    }
    setData(name, value) {
      this.data[name] = value;
    };
    getData(name) {
      return this.data[name];
    };
  }

  const cdnList = {
    socketIo: 'https://cdn.bootcdn.net/ajax/libs/socket.io/4.1.3/socket.io.min.js',
    axios: 'https://cdn.bootcdn.net/ajax/libs/axios/0.24.0/axios.min.js',
    zepto: 'https://cdn.bootcdn.net/ajax/libs/zepto/1.2.0/zepto.min.js',
  };

  $qkl.constants = $qkl.constants || {
    cdnList,
  };
  /* constants end */

  /* utils start */
  const taskManager = new TaskManager(window.location.host);

  const simulateInput = (input, value) => {
    const setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
    setter.call(input, value);

    const ev2 = new Event('input', { bubbles: true });
    input.dispatchEvent(ev2);
  }
  const simulateTextarea = (elem, value) => {
    const setter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value').set;
    setter.call(elem, value);

    const ev2 = new Event('input', { bubbles: true });
    elem.dispatchEvent(ev2);
  }
  const simulateSelect = (elem, value) => {
    const setter = Object.getOwnPropertyDescriptor(window.HTMLSelectElement.prototype, 'value').set;
    setter.call(elem, value);

    const ev2 = new Event('change', { bubbles: true });
    elem.dispatchEvent(ev2);
  }

  const loadCdn = async (src) => {
    await new Promise((resolve, reject) => {
      const script = document.createElement('script');
      script.src = src;
      document.getElementsByTagName('head')[0].appendChild(script);
      script.onload = resolve;
      script.onerror = reject;
    });
  };

  const delay = (time) => new Promise(resolve => setTimeout(resolve, time));

  const polling = (fn, endFn, interval) => new Promise(async resolve => {
    let intervalKey = null;
    let end = false;
    const wrapFn = async () => {
      if (end || endFn(await fn()) === true) {
        end = true;
        clearInterval(intervalKey);
        return resolve();
      }
    }
    await wrapFn();
    intervalKey = setInterval(wrapFn, interval);
  });

  const isElementVisible = (elem) => {
    if (!elem) return false;
    return !!(elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length );
  }

  $qkl.utils = $qkl.utils || {
    taskManager,
    simulateInput,
    simulateTextarea,
    simulateSelect,
    loadCdn,
    delay,
    polling,
    isElementVisible,
  };
  /* utils end */

  if (!$qkl.utils.taskManager.action) {
    console.log(`当前域名不支持: ${$qkl.utils.taskManager.host}`);
    return false;
  }
  if ($qkl.utils.taskManager.getData('inited')) {
    console.log('不要重复执行');
    return false;
  }
  $qkl.utils.taskManager.getData('inited', true);
  $qkl.utils.taskManager.action();
})();